package com.example.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.ResponseResult;
import com.example.domain.entity.Diary;
import com.example.exception.FloraException;
import com.example.service.DiaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/diary")
public class DiaryController {

    @Autowired
    private DiaryService diaryService;

    /** 查询植物日记 */
    /** 若传入日期，则会查询该日期之后的日记（格式：2022-11-30）
     *  状态、标题都是模糊查询
     */
    @GetMapping("/getDiaryByPlantId/{plantId}")
    public ResponseResult getDiaryByPlantId(@PathVariable Long plantId){
        return ResponseResult.okResult(diaryService.getDiaryByPlantId(plantId));
    }

    @GetMapping("/find")
    public ResponseResult findDiary(
            @RequestParam(required = true) Long plantId,
            @RequestParam(required = false)String createTime,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) String title,
            @RequestParam(required = false,defaultValue = "0")Integer pageNum,
            @RequestParam(required = false,defaultValue = "5")Integer pageSize
            ){
        Map<String,Object> condition = new HashMap<>();
        condition.put("plant_id",plantId);
        if(createTime!=null){
            condition.put("create_time",createTime);
        }
        if(state!=null){
            condition.put("state",state);
        }
        if(title!=null){
            condition.put("title",title);
        }

        return ResponseResult.okResult(diaryService.findDiary(condition,pageNum,pageSize));
    }


    /** 写植物日记，需要token请求头
     * @return*/
    @PostMapping("/addDiary")
    public Diary addDiary(@RequestBody Diary diary) throws FloraException {
        return diaryService.addDiary(diary);
    }


    /** 删除植物日记，需要token请求头 */
    @DeleteMapping("/deleteDiary")
    public void deleteDiary(@RequestParam Long id) throws FloraException {
        diaryService.deleteDiary(id);
    }


    /** 修改植物日记，需要token请求头 */
    @PutMapping("/updateDiary")
    public void updateDiary(@RequestBody Diary diary) throws FloraException {
        diaryService.updateDiary(diary);
    }

}
